package net.goutalk.glcs.module.bi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import net.goutalk.glcs.module.bi.dto.AddVisualDto;
import net.goutalk.glcs.module.bi.dto.VisualConfigDto;
import net.goutalk.glcs.module.bi.dto.VisualCopyDto;
import net.goutalk.glcs.module.bi.dto.VisualDto;
import net.goutalk.glcs.module.bi.entity.Visual;
import net.goutalk.glcs.module.bi.entity.VisualConfig;
import net.goutalk.glcs.module.bi.mapper.VisualMapper;
import net.goutalk.glcs.module.bi.service.IVisualConfigService;
import net.goutalk.glcs.module.bi.service.IVisualService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * BI 大屏列表 服务实现类
 * </p>
 *
 * @author zjq
 * @since 2022-09-21
 */
@Service
@AllArgsConstructor
public class VisualServiceImpl extends ServiceImpl<VisualMapper, Visual> implements IVisualService {

    private final IVisualConfigService visualConfigService;
    @Override
    public Map<String, Object> add(AddVisualDto dto) {
        VisualDto visualDto = dto.getVisual();
        Visual visual = BeanUtil.toBean(visualDto, Visual.class);
        VisualConfigDto configDto = dto.getConfig();
        VisualConfig visualConfig = BeanUtil.toBean(configDto, VisualConfig.class);
        this.save(visual);
        Long id = visual.getId();
        visualConfig.setVisualId(id);
        visualConfigService.save(visualConfig);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("id",id);
        return resultMap;
    }

    @Override
    public boolean updateVisual(Long id, Visual visual, VisualConfig visualConfig) {
        visual.setId(id);
        visualConfig.setVisualId(id);
        VisualConfig byVisualId = visualConfigService.getByVisualId(id);
        return this.updateById(visual) && visualConfigService.updateById(byVisualId);
    }

    @Override
    public Long copy(VisualCopyDto dto) {
        Visual visual = this.getById(dto.getId());
        long newVisualId = IdWorker.getId();
        visual.setId(newVisualId);
        if (StrUtil.isNotBlank(dto.getCategory().toString())) {
            visual.setCategory(dto.getCategory());
        }
        this.save(visual);
        VisualConfig visualConfig = visualConfigService.getByVisualId(dto.getId());
        visualConfig.setVisualId(newVisualId);
        visualConfig.setId(IdWorker.getId());
        visualConfigService.save(visualConfig);
      return newVisualId;
    }
}
